import { getsearchList } from '@/api/fixing/fixing'
import { getskuCollect, getItemTotal, getItemTotalMoney } from '@/api/fixing/fixState'
import dayjs from 'dayjs'
import { Loading } from 'element-ui'
export default {
  namespaced: true,
  state: {
    totalList: [],
    FixingStateTotal: {},
    TableList: [],
    dialogVisible: false,
    setskuCollectTable: [],
    innerCode: '',
    totalMoney: '',
    total: ''
  },
  mutations: {
    updataTableList (state, val) {
      state.TableList = val
    },
    updatatotal (state, val) {
      state.total = val
    },
    updatotalMoney (state, val) {
      state.totalMoney = val
    },
    updatadialogVisible (state) {
      state.dialogVisible = !state.dialogVisible
    },
    setFixingStateLsit (state, val) {
      const arr = val[0].currentPageRecords.map((item, index) => {
        return {
          ...item,
          num: index + 1 + (val[1] - 1) * 10,
          state:
            item.vmStatus === 1
              ? '运营'
              : '' || item.vmStatus === 0
                ? '未投放'
                : '' || item.vmStatus === 3
                  ? '撤机'
                  : ''
        }
      })
      state.TableList = arr
      state.FixingStateTotal = val
      state.innerCode = val[0].currentPageRecords[0].innerCode
      // console.log(state.FixingStateTotal);
      // console.log(state.TableList);
    },
    setFixingStateTotalLsit (state, val) {
      state.totalList = val
    },
    setskuCollect_vuex (state, val) {
      state.setskuCollectTable = val
      console.log(state.setskuCollectTable)
    }
  },
  actions: {
    // 总数据
    async getFixingStateTotalLsit (context, val) {
      const loadingInstance = Loading.service()
      const res = await getsearchList({
        pageIndex: val,
        pageSize: 10000
      })
      context.commit('setFixingStateTotalLsit', res.data.currentPageRecords)
      loadingInstance.close()
    },

    async getFixingStateLsit (context, val) {
      const loadingInstance = Loading.service()
      const res = await getsearchList({
        pageIndex: val,
        pageSize: 10
      })
      const num = val
      context.commit('setFixingStateLsit', [res.data, num])
      loadingInstance.close()
    },

    // 获取售货机商品销量
    async getskuCollect_vuex (context, val) {
      const innerCode = val.innerCode
      const end = dayjs(new Date()).format('YYYY-MM-DD')
      const start = dayjs(this.chooseMonth).startOf('month').format('YYYY-MM-DD')
      const res = await getskuCollect(
        {
          innerCode,
          start,
          end
        }
      )
      context.commit('setskuCollect_vuex', res.data)
    },
    // 获取一定时间范围之内的订单总数
    async getItemTotalFn (context, val) {
      const innerCode = val.innerCode
      const end = dayjs(new Date()).format('YYYY-MM-DD[ ][23:59:59]')
      const start = dayjs(this.chooseMonth).startOf('month').format('YYYY-MM-DD[ ][00:00:00]')
      const res = await getItemTotal({
        innerCode,
        start,
        end
      })
      context.commit('updatatotal', res.data)
    },
    // 获取一定时间范围之内的收入
    async getItemTotalMoneyFn (context, val) {
      const innerCode = val.innerCode
      const end = dayjs(new Date()).format('YYYY-MM-DD[ ][23:59:59]')
      const start = dayjs(this.chooseMonth).startOf('month').format('YYYY-MM-DD[ ][00:00:00]')
      const res = await getItemTotalMoney({
        innerCode,
        start,
        end
      })
      context.commit('updatotalMoney', (res.data) / 100)
    },
    handleClose (done) {
      this.$store.commit('fixingState/updatadialogVisible')
    }
  }
}
